﻿package net.wangit.framework.interceptor;

import net.wangit.Invocation;
import net.wangit.LabelUtil;
import net.wangit.context.ActionContext;
import net.wangit.context.ActionResult;
import net.wangit.framework.action.ActionMapping;
import net.wangit.framework.dispatcher.DefaultInvocation;
import net.wangit.framework.util.TokenProcessor;
import net.wangit.support.ActionInterceptor;
/**
 * 令牌校验
 * @author watson
 *
 */
public class TokenAI implements ActionInterceptor {

	public TokenAI() {
	}

	public void intercept(ActionContext ac, Invocation invocation)
			throws Exception {
		ActionMapping mapping = ((DefaultInvocation) invocation)
				.getActionMapping();
		if (!mapping.isTokenEnabled() || TokenProcessor.isTokenValid(ac)) {
			invocation.invoke();
		} else {
			ActionResult result = ac.getActionResult();
			result.setSuccessful(false);
			result
					.addError(LabelUtil.get(ac, "system",
							"action.invalid_token"));
		}
	}
}
